/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment1;

import java.io.*;
import java.util.StringTokenizer;
/**
 *
 * @author cjaramillo
 */
public class Rule {

    Consequent m_consequent;
    Antecedent m_antecedent;
    String m_description;
    
    Rule()
    {
        m_consequent = new Consequent();
        m_antecedent = new Antecedent();
        m_description = null;
    }
    
    public void populateFromString(String ruleString)
    {
        StringTokenizer tokenizer = new StringTokenizer(ruleString, ";");
        String antecedentAndConsequent = tokenizer.nextToken();
        m_description = tokenizer.nextToken();
        StringTokenizer partialTokenizer = new StringTokenizer(antecedentAndConsequent, "~");
        String antecedentString = partialTokenizer.nextToken();
        String consequentString = partialTokenizer.nextToken();
        m_antecedent.populateFromString(antecedentString);
        m_consequent.populateFromString(consequentString);
    }
    
    public String fire(Rover rover)
    {
        String log = "";
        switch(m_consequent.action())
        {
            case MOVE_FORWARD:
                log += rover.moveForward();
                break;
            case MOVE_BACKWARD:
                log += rover.moveBackward();
                break;
            case ROTATE_CLOCKWISE:
                log += rover.rotateClockwise();
                break;
            case ROTATE_COUNTERCLOCKWISE:
                log += rover.rotateCounterclockwise();
                break;
            case ORIENT_ROVER_TOWARD_TARGET:
                log += rover.orientRoverTowardTarget();
                break;
            case FOLLOW_PATH_TO_TARGET:
                rover.followPathToTarget();
                break;
            case CREATE_SUBGOAL_LIST:
                rover.createSubgoalList();
                break;
            case REMOVE_OBSTACLES_FROM_SUBGOALS:
                rover.removeObstaclesFromSubgoals();
                break;
            case REMOVE_POSITION_FROM_SUBGOALS:
                rover.removePositionFromSubgoals();
                break;
            case FOLLOW_PATH_TO_SUBGOAL:
                rover.followPathToSubgoal();
                break;
        }
        return log;
    }
    
    public boolean evaluate(Database database)
    {
        if(m_antecedent.evaluate(database))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void setConsequentFromUserInput() throws IOException
    {
        m_consequent = new Consequent();
        m_consequent.setConsequentFromUserInput();
    }
    public String toString()
    {
        return m_description;
    }
}
